package com.example.component.d_content_provider.custom

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.contentValuesOf
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.component.databinding.ActivityDCBinding

class DCActivity : AppCompatActivity() {
    private lateinit var binding: ActivityDCBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityDCBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val dbHelper = MyDatabaseHelper(this, "BookStore.db", 2)
        dbHelper.writableDatabase

        initButtons()
    }

    private fun initButtons() {
        var bookId: String? = null

        /**
         * content provider CRUD
         */
        binding.addData.setOnClickListener {
            // 添加数据
            val uri = "content://com.example.component.provider/book".toUri()
            val values = contentValuesOf(
                "name" to "A Clash of Kings",
                "author" to "George Martin",
                "pages" to 1040,
                "price" to 22.85
            )
            val newUri = contentResolver.insert(uri, values)
            bookId = newUri?.pathSegments?.get(1)
        }
        binding.queryData.setOnClickListener {
            // 查询数据
            val uri = "content://com.example.component.provider/book".toUri()
            contentResolver.query(uri, null, null, null, null)?.apply {
                while (moveToNext()) {
                    val name = getString(getColumnIndexOrThrow("name"))
                    val author = getString(getColumnIndexOrThrow("author"))
                    val pages = getInt(getColumnIndexOrThrow("pages"))
                    val price = getDouble(getColumnIndexOrThrow("price"))
                    val msg =
                        "book name is $name, author is $author, pages is $pages, price is $price"
                    Log.d(TAG, msg)
                }
                close()
            }
        }
        binding.updateData.setOnClickListener {
            // 更新数据
            bookId?.let {
                val uri = "content://com.example.component.provider/book/$it".toUri()
                val values = contentValuesOf(
                    "name" to "A Storm of Swords",
                    "pages" to 1216,
                    "price" to 24.05
                )
                contentResolver.update(uri, values, null, null)
            }
        }
        binding.deleteData.setOnClickListener {
            // 删除数据
            bookId?.let {
                val uri = "content://com.example.component.provider/book/$it".toUri()
                contentResolver.delete(uri, null, null)
            }
        }
    }

    companion object {
        private const val TAG = "DCActivity"
    }
}